import { VxeGlobalInterceptorHandles } from "../../../../../types/all";
import ElAutoComplete from "./ElAutoComplete";
import ElInput from "./ElInput";
import ElInputNumber from "./ElInputNumber";
import ElSelect from "./ElSelect";
import ElCascader from "./ElCascader";
import ElDatePicker from "./ElDatePicker";
import ElTreeSelect from "./ElTreeSelect";
import ElTimePicker from "./ElTimePicker";
import ElRate from "./ElRate";
import ElSwitch from "./ElSwitch";
import ElRadioGroup from "./ElRadioGroup";
import ElCheckboxGroup from "./ElCheckboxGroup";
import ElButton from "./ElButton";
import ElButtonGroup from "./ElButtonGroup";
import ElApiSelect from "./ElApiSelect";
import ElApiTreeSelect from "./ElApiTreeSelect";
import ElEmpty from "./ElEmpty";
import ElInputSearch from "./ElInputSearch";
import MonacoEditor from "./MonacoEditor";
import ElDictTag from "./ElDictTag";
import ElTimeSelect from "./ElTimeSelect";

/**
 * 检查触发源是否属于目标节点
 */
function getEventTargetNode(evnt: any, container: HTMLElement, className: string) {
    let targetElem;
    let target = evnt.target;
    while (target && target.nodeType && target !== document) {
        if (className && target.className && target.className.split && target.className.split(" ").indexOf(className) > -1) {
            targetElem = target;
        } else if (target === container) {
            return { flag: className ? !!targetElem : true, container, targetElem: targetElem };
        }
        target = target.parentNode;
    }
    return { flag: false };
}

/**
 * 事件兼容性处理
 */
function handleClearEvent(params: VxeGlobalInterceptorHandles.InterceptorClearFilterParams | VxeGlobalInterceptorHandles.InterceptorClearActivedParams | VxeGlobalInterceptorHandles.InterceptorClearAreasParams) {
    const { $event } = params;
    const bodyElem = document.body;
    if (
        // 远程搜索
        getEventTargetNode($event, bodyElem, "el-autocomplete-suggestion").flag ||
        // 下拉框
        getEventTargetNode($event, bodyElem, "el-select-dropdown").flag ||
        // 级联
        getEventTargetNode($event, bodyElem, "el-cascader__dropdown").flag ||
        getEventTargetNode($event, bodyElem, "el-cascader-menus").flag ||
        // 日期
        getEventTargetNode($event, bodyElem, "el-time-panel").flag ||
        getEventTargetNode($event, bodyElem, "el-picker-panel").flag ||
        // 颜色
        getEventTargetNode($event, bodyElem, "el-color-dropdown").flag
    ) {
        return false;
    }
}

/**
 * 基于 vxe-table 表格的适配插件，用于兼容 element-ui 组件库
 */
export const VXETablePluginAntd = {
    install(vxetablecore: any) {
        const { interceptor, renderer } = vxetablecore;

        renderer.mixin({
            ElAutoComplete,
            ElInput,
            ElInputNumber,
            ElSelect,
            ElCascader,
            ElDatePicker,
            ElTimePicker,
            ElTimeSelect,
            ElTreeSelect,
            ElRate,
            ElSwitch,
            ElRadioGroup,
            ElCheckboxGroup,
            ElButton,
            ElButtonGroup,
            ElApiSelect,
            ElApiTreeSelect,
            ElEmpty,
            ElInputSearch,
            MonacoEditor,
            ElDictTag,
        });

        interceptor.add("event.clearFilter", handleClearEvent);
        interceptor.add("event.clearActived", handleClearEvent);
        interceptor.add("event.clearAreas", handleClearEvent);
    },
};

if (typeof window !== "undefined" && window.VXETable && window.VXETable.use) {
    window.VXETable.use(VXETablePluginAntd);
}

export default VXETablePluginAntd;
